import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

///输入框
class InPutDemo extends StatelessWidget {
  final String title;

  const InPutDemo({Key? key, required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("$title"),
        centerTitle: true,
      ),
      body: InputWidget(),
    );
  }
}

class InputWidget extends StatefulWidget {
  const InputWidget({Key? key}) : super(key: key);

  @override
  _InputWidgetState createState() => _InputWidgetState();
}

class _InputWidgetState extends State<InputWidget> {
  GlobalKey _key = GlobalKey<FormState>();

  //管理值的改变
  TextEditingController _nickController = TextEditingController();
  TextEditingController _pwdController = TextEditingController();

  FocusNode _n = FocusNode();
  FocusNode _p = FocusNode();
  late FocusScopeNode _scopeNode;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    _nickController.dispose();
    _pwdController.dispose();
    _n.dispose();
    _p.dispose();
    _scopeNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _scopeNode = FocusScope.of(context);
    return Form(
        key: _key,
        //开启表单校验
        autovalidate: true,
        child: Column(
          children: [
            TextFormField(
              validator: (v) {
                if (v == null || v.length == 0) {
                  return "账号必须输入";
                }
              },
              controller: _nickController,
              onChanged: (v) {
                // print("v的value：" + v);
              },
              //装饰器
              decoration: InputDecoration(
                  // icon: Icon(Icons.add),
                  labelText: "账号",
                  prefixIcon: Icon(Icons.add),
                  hintText: "default"),
              textInputAction: TextInputAction.next,
              keyboardType: TextInputType.text,
              //自动获取焦点
              autofocus: true,
              focusNode: _n,
              onFieldSubmitted: (v) {
                print(
                    "onFieldSubmitted：  textInputAction:TextInputAction.next回调:  " +
                        v.toString());
              },
            ),
            SizedBox(
              height: 8,
            ),
            TextFormField(
              validator: (v) {
                if (v == null || v.length < 5) {
                  return "密码输入错误,必须大于五位数";
                }
              },
              controller: _pwdController,
              //装饰器
              decoration: InputDecoration(
                  labelText: "密码",
                  prefixIcon: Icon(Icons.add),
                  hintText: "default"),
              textInputAction: TextInputAction.done,
              onFieldSubmitted: (v) {
                // textInputAction:TextInputAction.done回调
                print(
                    "onFieldSubmitted：  textInputAction:TextInputAction.done回调:  " +
                        v.toString());
              },
              keyboardType: TextInputType.number,
              //是否为密码框
              obscureText: true,
              focusNode: _p,
            ),
            SizedBox(
              height: 16,
            ),
            RaisedButton(
              onPressed: () {
                //提交的时候验证validator
                if ((_key.currentState as FormState).validate()) {
                  //执行登录
                  //点击提交获取输入nickName的焦点
                  // _scopeNode.requestFocus(_n);
                  //清空所以焦点
                  _scopeNode.unfocus();
                }
              },
              child: Text("提交"),
              color: Colors.blue,
            ),
          ],
        ));
  }
}
